#include "AudioFrame.h"

extern "C" {
    #include <libavutil/frame.h>
}

AudioFrame::AudioFrame(AVFrame* frame,int64_t ptsMs)
    : m_frame(frame)
    , m_ptsMs(ptsMs)
{

}


AudioFrame::~AudioFrame()
{
    release();
}


// 移动构造函数
AudioFrame::AudioFrame(AudioFrame&& other) noexcept
    : m_frame(other.m_frame)
    , m_ptsMs(other.m_ptsMs)
{
    other.m_frame = nullptr;
    other.m_ptsMs = 0;
}

// 移动赋值运算符
AudioFrame& AudioFrame::operator=(AudioFrame&& other) noexcept
{
    if(this != &other)
    {
        release();
        m_frame = other.m_frame;
        m_ptsMs = other.m_ptsMs;
        other.m_frame = nullptr;
        other.m_ptsMs = 0;
    }
    return *this;
}

bool AudioFrame::isValid() const
{
    return m_frame!=nullptr;
}

AVFrame* AudioFrame::getRawFrame() const
{
    return m_frame;
}

int AudioFrame::getSampleRate() const
{
    return m_frame ? m_frame->sample_rate : 0;
}

int AudioFrame::getChannels() const
{
    return m_frame ? m_frame->ch_layout.nb_channels : 0;
}


int AudioFrame::getNbSamples() const
{
    return m_frame ? m_frame->nb_samples : 0;
}

int AudioFrame::getFormat() const
{
    return m_frame ? m_frame->format : -1;
}

int64_t AudioFrame::getPtsMs() const
{
    return m_ptsMs;
}


void AudioFrame::setPtsMs(int64_t ptsMs)
{
    m_ptsMs=ptsMs;
}

uint8_t** AudioFrame::getData() const
{
    return m_frame ? m_frame->data : nullptr;
}

int* AudioFrame::getLinesize() const
{
    return m_frame ? m_frame->linesize : nullptr;
}

void AudioFrame::release()
{
    if(m_frame)
    {
        av_frame_free(&m_frame);
        m_frame=nullptr;
    }
    m_ptsMs=0;
}


